package com.movitech.mobile.cz.base.sys.service.impl;

import com.movitech.mobile.cz.base.common.aop.ServiceThrowable;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.sys.bizvo.SRegionalCompanyAreaUpdateRequestVO;
import com.movitech.mobile.cz.base.sys.entity.SRegionalCompanyArea;
import com.movitech.mobile.cz.base.sys.mapper.SRegionalCompanyAreaMapper;
import com.movitech.mobile.cz.base.sys.mapper.ext.ExtSRegionalCompanyAreaMapper;
import com.movitech.mobile.cz.base.sys.service.SRegionalCompanyAreaService;
import com.movitech.mobile.cz.base.util.UserUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 地区公司与地区的关联关系 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-04-26
 */
@Service
public class SRegionalCompanyAreaServiceImpl extends ServiceImpl<SRegionalCompanyAreaMapper, SRegionalCompanyArea> implements SRegionalCompanyAreaService {

	private static final Logger logger = Logger.getLogger(SRegionalCompanyAreaServiceImpl.class);

    @Autowired
    private ExtSRegionalCompanyAreaMapper extSRegionalCompanyAreaMapper;


	/**
	 * 地区公司与地区的关联关系 更新
	 */
	@Override
	public RestReturnResult saveOrUpdateRegionalCompanyArea(SRegionalCompanyAreaUpdateRequestVO vo) {
		
		try{
			
			if(vo != null
					&& StringUtils.isNotBlank(vo.getRegionalCompanyId())){
				
				SRegionalCompanyArea areaDB = this.selectSRegionalCompanyAreaByCompanyId(vo.getRegionalCompanyId());
				if(areaDB != null){ //update
					areaDB.setAreaIds(vo.getAreaIds());
					areaDB.setBaseFeilds(UserUtils.getCurrentUserId(), null, CommonConstants.NO_INTEGER, false);
					
					this.updateAllColumnById(areaDB);
					
					RestReturnResult result = new RestReturnResult(CommonConstants.SUCCESS, "修改成功");
					
					return result;
				}else{//insert
					SRegionalCompanyArea area = new SRegionalCompanyArea();
					area.setRegionalCompanyId(vo.getRegionalCompanyId());
					area.setAreaIds(vo.getAreaIds());
					area.setBaseFeilds(UserUtils.getCurrentUserId(), null, CommonConstants.NO_INTEGER, true);
					
					this.insert(area);
					
					RestReturnResult result = new RestReturnResult(CommonConstants.SUCCESS, "新增成功");
					
					return result;
				}
				
			}else{
				throw new RuntimeException("传参为空", new ServiceThrowable());
			}
			
		}catch(Exception e){
			e.printStackTrace();
			logger.error("==========saveOrUpdateRegionalCompanyArea===========e:===" + e.getMessage());
			throw new RuntimeException(e.getMessage(), e.getCause());
		}
		
	}

	private SRegionalCompanyArea selectSRegionalCompanyAreaByCompanyId(String regionalCompanyId) {
		
		SRegionalCompanyArea param = new SRegionalCompanyArea();
		param.setRegionalCompanyId(regionalCompanyId);
		param.setDelFlag(CommonConstants.NO_INTEGER);
		
		EntityWrapper wapper = new EntityWrapper();
		wapper.setEntity(param);
		
		return this.selectOne(wapper);
	}
	
    @Override
    public String getProvinceAreas(String provinceId) {
        return extSRegionalCompanyAreaMapper.getProvinceAreas(provinceId);
    }
}
